package com.googlecode.bip.xml.importe;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import javax.naming.NamingException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import com.googlecode.bip.entity.IllegalIdLocalException;
import com.googlecode.bip.entity.IllegalValueException;

public class ImportXMLSurveyFactory
{
    private ImportXMLSurveyFactory()
    {
    }

    /**
     *
     * @param xmlIn
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     * @throws SQLException
     * @throws NamingException
     * @throws IllegalIdLocalException 
     * @throws IllegalValueException 
     */
    public static void importXML( InputStream xmlIn )
            throws  ParserConfigurationException,
                    SAXException,
                    IOException,
                    NamingException,
                    SQLException,
                    IllegalIdLocalException,
                    IllegalValueException
    {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // Create the builder and parse the file
    Document doc = factory.newDocumentBuilder().parse( xmlIn );

    SQLStatementCollection sqlCollection = new SQLStatementCollection();
    SurveyScanner survey        = new SurveyScanner( sqlCollection , doc);
    survey.visitDocument();
    survey = null;

    sqlCollection.performCommits();
    }

    public static void importXML( File xmlIn )
            throws FileNotFoundException,
                   ParserConfigurationException,
                   SAXException,
                   IOException,
                   NamingException,
                   SQLException,
                   IllegalIdLocalException,
                   IllegalValueException
    {
          importXML( new FileInputStream( xmlIn ));
      }

}
